package net.caixiaomi.info.ui.discovery;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import net.caixiaomi.info.R;
import net.caixiaomi.info.base.BaseFragment;
import net.caixiaomi.info.base.quickadapter.BaseQuickAdapter;
import net.caixiaomi.info.base.quickadapter.BaseViewHolder;
import net.caixiaomi.info.base.quickadapter.entity.MultiItemEntity;
import net.caixiaomi.info.interfaces.ComonFragmentImpl;
import net.caixiaomi.info.model.DIPlayerEntity;
import net.caixiaomi.info.model.DIPlayerInfo;
import net.caixiaomi.info.model.DividerEntity;
import net.caixiaomi.info.ui.view.DividerParser;
import net.caixiaomi.info.ui.view.DividerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by mac on 2018/9/11.
 */

public class TeamPersonListFragment extends BaseFragment implements ComonFragmentImpl {

    @BindView(android.R.id.list)
    RecyclerView mListView;

    @BindView(R.id.no_data_tv)
    TextView mNoDataTv;

    private LeagueTeamAdapter mAdapter;

    private LeagueTeamAdapter mAdapter0;
    private LeagueTeamAdapter mAdapter1;
    private LeagueTeamAdapter mAdapter2;
    private LeagueTeamAdapter mAdapter3;

    RecyclerView mListView0;
    RecyclerView mListView1;
    RecyclerView mListView2;
    RecyclerView mListView3;

    private DIPlayerEntity entity;

    private View mHeaderView0;
    private View mHeaderView1;
    private View mHeaderView2;
    private View mHeaderView3;

    private LinearLayout noData0Tv;
    private LinearLayout noData1Tv;
    private LinearLayout noData2Tv;
    private LinearLayout noData3Tv;

    private View no_data_view0;
    private View no_data_view1;
    private View no_data_view2;
    private View no_data_view3;

    @Override
    public void setType(int type) {

    }

    @Override
    public void setFilter(String filter) {

    }

    @Override
    protected int fragmentLayout() {
        return R.layout.league_shooter_layout;
    }

    @Override
    protected void onFragmentCreated(View view, @Nullable Bundle savedInstanceState) {

        mListView.setLayoutManager(new GridLayoutManager(getActivity(),2));
        mAdapter = new LeagueTeamAdapter(R.layout.discovery_main_select_item);
        mListView.setAdapter(mAdapter);
        mHeaderView0=View.inflate(getActivity(),R.layout.team_grid_item_layout,null);
        mHeaderView1=View.inflate(getActivity(),R.layout.team_grid_item_layout,null);
        mHeaderView2=View.inflate(getActivity(),R.layout.team_grid_item_layout,null);
        mHeaderView3=View.inflate(getActivity(),R.layout.team_grid_item_layout,null);
        noData0Tv=mHeaderView0.findViewById(R.id.no_data_tv);
        noData1Tv=mHeaderView1.findViewById(R.id.no_data_tv);
        noData2Tv=mHeaderView2.findViewById(R.id.no_data_tv);
        noData3Tv=mHeaderView3.findViewById(R.id.no_data_tv);
        no_data_view0=mHeaderView0.findViewById(R.id.no_data_view);
        no_data_view1=mHeaderView1.findViewById(R.id.no_data_view);
        no_data_view2=mHeaderView2.findViewById(R.id.no_data_view);
        no_data_view3=mHeaderView3.findViewById(R.id.no_data_view);
        TextView tv0=mHeaderView0.findViewById(R.id.team_name_title);
        TextView tv1=mHeaderView1.findViewById(R.id.team_name_title);
        TextView tv2=mHeaderView2.findViewById(R.id.team_name_title);
        TextView tv3=mHeaderView3.findViewById(R.id.team_name_title);
        mListView0=mHeaderView0.findViewById(R.id.rerlist);
        mListView1=mHeaderView1.findViewById(R.id.rerlist);
        mListView2=mHeaderView2.findViewById(R.id.rerlist);
        mListView3=mHeaderView3.findViewById(R.id.rerlist);
        tv0.setText("守门员");
        tv1.setText("后卫");
        tv2.setText("中场");
        tv3.setText("前锋");
        mAdapter.addHeaderView(mHeaderView0);
        mAdapter.addHeaderView(mHeaderView1);
        mAdapter.addHeaderView(mHeaderView2);
        mAdapter.addHeaderView(mHeaderView3);

        mListView0.setLayoutManager(new GridLayoutManager(getActivity(),2));
        mAdapter0 = new LeagueTeamAdapter(R.layout.textview_item);
        mListView0.setAdapter(mAdapter0);

        mListView1.setLayoutManager(new GridLayoutManager(getActivity(),2));
        mAdapter1 = new LeagueTeamAdapter(R.layout.textview_item);
        mListView1.setAdapter(mAdapter1);

        mListView2.setLayoutManager(new GridLayoutManager(getActivity(),2));
        mAdapter2 = new LeagueTeamAdapter(R.layout.textview_item);
        mListView2.setAdapter(mAdapter2);

        mListView3.setLayoutManager(new GridLayoutManager(getActivity(),2));
        mAdapter3 = new LeagueTeamAdapter(R.layout.textview_item);
        mListView3.setAdapter(mAdapter3);
    }

    public void initData(DIPlayerEntity entity){
        this.entity=entity;
        List<DIPlayerInfo> list0=entity.getGoalKeepers().getPlayerList();
        List<DIPlayerInfo> list1=entity.getBackPlayers().getPlayerList();
        List<DIPlayerInfo> list2=entity.getMidPlayers().getPlayerList();
        List<DIPlayerInfo> list3=entity.getForwards().getPlayerList();
        if(list0.size()==0&&list1.size()==0&&list2.size()==0&&list3.size()==0){
//            mListView.setVisibility(View.GONE);
//            mNoDataTv.setVisibility(View.VISIBLE);
//            return;
        }

        for(int i=0;i<list0.size()%2;i++){
            DIPlayerInfo ee=new DIPlayerInfo();
            ee.empty=true;
            list0.add(ee);
        }
        for(int i=0;i<list1.size()%2;i++){
            DIPlayerInfo ee=new DIPlayerInfo();
            ee.empty=true;
            list1.add(ee);
        }
        for(int i=0;i<list2.size()%2;i++){
            DIPlayerInfo ee=new DIPlayerInfo();
            ee.empty=true;
            list2.add(ee);
        }
        for(int i=0;i<list3.size()%2;i++){
            DIPlayerInfo ee=new DIPlayerInfo();
            ee.empty=true;
            list3.add(ee);
        }

        List<DividerEntity>divider0=new ArrayList<>();
        List<DividerEntity>divider1=new ArrayList<>();
        List<DividerEntity>divider2=new ArrayList<>();
        List<DividerEntity>divider3=new ArrayList<>();
        divider0.addAll(list0);
        divider1.addAll(list1);
        divider2.addAll(list2);
        divider3.addAll(list3);
        new DividerParser().parseDivider(divider0,2);
        new DividerParser().parseDivider(divider1,2);
        new DividerParser().parseDivider(divider2,2);
        new DividerParser().parseDivider(divider3,2);
        //------

        List<MultiItemEntity>data0=new ArrayList<>();
        List<MultiItemEntity>data1=new ArrayList<>();
        List<MultiItemEntity>data2=new ArrayList<>();
        List<MultiItemEntity>data3=new ArrayList<>();

        data0.addAll(list0);
        data1.addAll(list1);
        data2.addAll(list2);
        data3.addAll(list3);

        if(data0.size()==0){
            noData0Tv.setVisibility(View.VISIBLE);
        }else {
            noData0Tv.setVisibility(View.GONE);
            no_data_view0.setVisibility(View.GONE);
        }
        if(data1.size()==0){
            noData1Tv.setVisibility(View.VISIBLE);
        }else {
            no_data_view1.setVisibility(View.GONE);
            noData1Tv.setVisibility(View.GONE);
        }
        if(data2.size()==0){
            noData2Tv.setVisibility(View.VISIBLE);
        }else {
            noData2Tv.setVisibility(View.GONE);
            no_data_view2.setVisibility(View.GONE);
        }
        if(data3.size()==0){
            noData3Tv.setVisibility(View.VISIBLE);
        }else {
            noData3Tv.setVisibility(View.GONE);
            no_data_view3.setVisibility(View.GONE);
        }

        mAdapter0.setNewData(data0);
        mAdapter1.setNewData(data1);
        mAdapter2.setNewData(data2);
        mAdapter3.setNewData(data3);
    }

    public class LeagueTeamAdapter extends BaseQuickAdapter<MultiItemEntity, BaseViewHolder> {

        public LeagueTeamAdapter(int layoutResId) {
            super(layoutResId);
        }

        @Override
        protected void convert(BaseViewHolder helper, MultiItemEntity item) {
            try {
                DividerView dividerView=helper.getView(R.id.divider_view);
                DividerEntity divider= (DividerEntity) item;
                for(int i=0;i<divider.dividerType.size();i++){
                    dividerView.setStyle(divider.dividerType.get(i));
                }
                if(divider.empty){
                    dividerView.setChildViewGone();
                    return;
                }

                //---
                DIPlayerInfo hall = (DIPlayerInfo) item;
                String name = hall.getPlayerNo()+"号-"+hall.getPlayerName();
                helper.setText(R.id.title, name);

            }catch (Exception e){

            }
        }
    }
}
